// import 'package:cached_network_image/cached_network_image.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
// import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';

/// A mixin that can render IMG with `cached_network_image` plugin.
mixin CachedNetworkImageFactory on WidgetFactory {
  /// Uses a custom cache manager.
  dynamic get cacheManager => null;

  @override
  Widget? buildImageWidget(BuildMetadata meta, ImageSource src) {
    final url = src.url;
    if (!url.startsWith(RegExp('https?://'))) {
      return super.buildImageWidget(meta, src);
    }

    return ExtendedImage.network(
      url,
      loadStateChanged: (ExtendedImageState state) {
        switch (state.extendedImageLoadState) {
          case LoadState.loading:
            return errorWidget();
          case LoadState.completed:
            return state.completedWidget;
          case LoadState.failed:
            return errorWidget();
        }
      },
    );
    // return CachedNetworkImage(
    //   cacheManager: cacheManager,
    //   errorWidget: (context, _, error) =>
    //       onErrorBuilder(context, meta, error, src) ?? widget0,
    //   fit: BoxFit.fill,
    //   imageUrl: url,
    //   progressIndicatorBuilder: (context, _, progress) =>
    //       onLoadingBuilder(
    //         context,
    //         meta,
    //         progress.totalSize != null && progress.totalSize! > 0
    //             ? progress.downloaded / progress.totalSize!
    //             : null,
    //         src,
    //       ) ??
    //       widget0,
    // );
  }

  Widget errorWidget() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.grey.withOpacity(0.2),
      ),
    );
  }
}
